import axios from 'axios'

//创建axios实例
let axiosObj = axios.create({
    baseURL: 'http://10.0.38.238:3000'
})

//请求拦截器
axiosObj.interceptors.request.use(config=>{
    return config
},err=>{
    return Promise.reject(err)
})

//响应拦截
axiosObj.interceptors.response.use(res=>{
    return res
},err=>{
    return Promise.reject(err)
})

//统一请求方法
async function http({method='get',path,params}){
    let result 
    if(method === 'get' || method === 'delete'){
        await axiosObj[method](path,{
            params
        }).then(res=>{
            result = res
        }).catch(err=>{
            result = err
        })
    }else if(method === 'post' || method === 'put' || method === 'patch'){
        await axiosObj[method](path,params).then(res=>{
            result = res
        }).catch(err=>{
            result = err
        })
    }

    return result
}

//封装并发请求
// async function all(...request){

//     let results = []

//     let req = []
//     request.forEach(item=>{
//         req.push(item)
//     })

//     await axios.all(req).then(
//         axios.spread((...res)=>{
//             results = res
//         })
//     )

//     return results
// }

// export default {
//     http,
//     all
// }

export default http